package com.anxuan.wiki.service;


import com.anxuan.wiki.domain.User;
import com.anxuan.wiki.domain.UserExample;
import com.anxuan.wiki.exception.BusinessException;
import com.anxuan.wiki.exception.BusinessExceptionCode;
import com.anxuan.wiki.mapper.UserMapper;
import com.anxuan.wiki.req.UserLoginReq;
import com.anxuan.wiki.req.UserQueryReq;
import com.anxuan.wiki.req.UserResetPasswordReq;
import com.anxuan.wiki.req.UserSaveReq;
import com.anxuan.wiki.resp.UserLoginResp;
import com.anxuan.wiki.resp.UserQueryResp;
import com.anxuan.wiki.resp.PageResp;
import com.anxuan.wiki.util.CopyUtil;
import com.anxuan.wiki.util.SnowFlake;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Slf4j
@Service
public class UserService {
    @Resource
    private UserMapper userMapper;
    @Resource
    private SnowFlake snowFlake;

    public PageResp<UserQueryResp> list(UserQueryReq req) {

        UserExample userExample = new UserExample();
        UserExample.Criteria criteria = userExample.createCriteria();
        if (!ObjectUtils.isEmpty(req.getLoginName())) {

            criteria.andLoginNameEqualTo(req.getLoginName());
        }
        PageHelper.startPage(req.getPage(), req.getSize());
        List<User> userList = userMapper.selectByExample(userExample);
        PageInfo<User> pageInfo = new PageInfo<>(userList);
        log.info("总页数：{}",pageInfo.getTotal());
        log.info("总页数：{}",pageInfo.getPages());


        /*
        持久层返回List<User>
        需要转换成 List<UserResp>
        再返回controller
         */

       /* for (User user:userList) {
         对象复制   UserResp userResp = CopyUtil.copy(user, UserResp.class);
            respList.add(userResp);
        List<UserQueryResp> respList = new ArrayList<>();
        }*/

        List<UserQueryResp> list = CopyUtil.copyList(userList, UserQueryResp.class);
        PageResp<UserQueryResp> pageResp = new PageResp<>();
        pageResp.setTotal(pageInfo.getTotal());
        pageResp.setList(list);
        return pageResp;


    }

    //保存
    public void save(UserSaveReq req) {
        User user = CopyUtil.copy(req, User.class);
        if (ObjectUtils.isEmpty(req.getId())) {

            //新增
            //user.setId(snowFlake.nextId());
            //userMapper.insert(user);
            User userDB = selectByLoginName(req.getLoginName());
            if(ObjectUtils.isEmpty(userDB)){
               user.setId(snowFlake.nextId());
               userMapper.insert(user);
            }else{
                throw new BusinessException(BusinessExceptionCode.USER_LOGIN_NAME_EXIST);
            }

        } else {
            //更新
            user.setLoginName(null);
            user.setPassword(null);
            userMapper.updateByPrimaryKeySelective(user);
        }


    }

    public User selectByLoginName(String LoginName){
        UserExample userExample = new UserExample();
        UserExample.Criteria criteria = userExample.createCriteria();
        criteria.andLoginNameEqualTo(LoginName);
        List<User> userList = userMapper.selectByExample(userExample);
        if(CollectionUtils.isEmpty(userList)){
            return null;
        }else{
            return userList.get(0);
        }
    }


    public void delete(Long id) {
        userMapper.deleteByPrimaryKey(id);
    }

    //重置密码
    public void resetPassword(UserResetPasswordReq req) {
        User user = CopyUtil.copy(req,User.class);
        userMapper.updateByPrimaryKeySelective(user);
    }

    //登录
    public UserLoginResp login(UserLoginReq req) {
        User userDb = selectByLoginName(req.getLoginName());
        if(ObjectUtils.isEmpty(userDb)){
            log.info("用户名不存在,{}",req.getLoginName());
            throw new BusinessException(BusinessExceptionCode.LOGIN_USER_ERROR);
        }else{
            if(userDb.getPassword().equals(req.getPassword())){
                UserLoginResp userLoginResp = CopyUtil.copy(userDb,UserLoginResp.class);
                return userLoginResp;
            }else{
                log.info("密码不对, 输入密码：{}, 数据库密码：{}", req.getPassword(), userDb.getPassword());
                throw new BusinessException(BusinessExceptionCode.LOGIN_USER_ERROR);
            }
        }
    }

}
